<?php
/**
 * Created by PhpStorm.
 * User: 木易
 * Date: 2019/1/9
 * Time: 11:19
 */
namespace app\admin\controller;
use think\Request;
use think\Db;
class Article extends Base
{
    public function index(){
        $query = "select * ,(case status when 0 then '停用' else '启用' end ) as stats from article";
        $status=Db::query($query);
        $res=Db::table('article')->join('category' ,'category.id = article.cid')->field('article.*,category.id as category_id,category.type,category.cate')->order('article.id','desc')->paginate(8);
        $cate=Db::table('category')->where('status',1)->select();
        $this->assign('cate',$cate);
        $this->assign('res',$res);
        $this->assign('status',$status);
        return $this->fetch();
    }
    public function addarticle(){
        $res=Db::table('category')->where('status',1)->select();
        $this->assign('res',$res);
        return $this->fetch();
    }
    //    将添加的文章存入数据库
    public function save(Request $request)
    {
        //处理增加数据
        $data=input("post.");
        $image=$request->file('image');
        if($image){
            $info = $image->move(ROOT_PATH . 'public' . DS . 'static'. DS . 'uploads');
            $Suffix=$info->getExtension();
            $arr=['jpeg','jpg','gif','png'];
            if (in_array($Suffix,$arr)){
                if($info){
                    // 输出 存入数据库的名
                    $savename=$info->getSaveName();
                    $data=array_merge(['image'=>$savename],$data);
                    Db::table('article')->insert($data);
                }else{
                    // 上传失败获取错误信息
                    echo $image->getError();
                }
            }else{
                $this->error('上传文件格式不正确！');
            }
        }

    }
//    修改文章
    public function update_article(){
        $id=input('id');
        $data=Db::table('article')->where('id',$id)->find();
        $cate=Db::table('category')->where('status',1)->select();
        $this->assign('data',$data);
        $this->assign('cate',$cate);
        return $this->fetch();
    }
//    文章修改存入数据库
    public  function  save_up(Request $request ){
        //获取表单传过来的数据
        $data=input('post.');
        $id=input('post.id');
        $image=$request->file('image');
        if($image){
            $info = $image->move(ROOT_PATH . 'public' . DS . 'static'. DS . 'uploads');
            $Suffix=$info->getExtension();
            $arr=['jpeg','jpg','gif','png'];
            if (in_array($Suffix,$arr)){
                if($info){
                    // 输出 存入数据库的名
                    $savename=$info->getSaveName();
                    $data=array_merge(['image'=>$savename],$data);
                    Db::table('article')->where('id',$id)->update($data);
                    $this->redirect('/admin/article/article');
                }else{
                    // 上传失败获取错误信息
                    echo $image->getError();
                }
            }
        }else{
            Db::table('article')->where('id',$id)->update($data);
            $this->redirect('/admin/article/article');
        }
    }

    //删除文章
    public function del(){
        $id=input('post.id');
        $res=Db::table('article')->where('id',$id)->delete();
        if ($res){
            return 1;
        }else{
            return '删除失败';
        }
    }
}